package com.cx.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mapper.LolBattleHeaderMapper;
import com.cx.po.LolBattleHeaderPo;
import com.cx.service.LolBattleHeaderService;
import com.cx.vo.LolBattleHeaderVo;
import com.cx.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 残星
 * @Description LOL-对局基本信息 业务逻辑类
 * @date 2022-11-30
 */
@Service
public class LolBattleHeaderServiceImpl extends ServiceImpl<LolBattleHeaderMapper, LolBattleHeaderPo> implements LolBattleHeaderService {
    @Resource(name = "mongoTemplate")
    private MongoTemplate mongoTemplate;
    @Resource(name = "lolBattleHeaderMapper")
    private LolBattleHeaderMapper lolBattleHeaderMapper;

    @Override
    public Page<LolBattleHeaderPo> findLolHeaderPage(Page<LolBattleHeaderPo> page) {
        return lolBattleHeaderMapper.findLolHeaderPage(page);
    }

    @Override
    public ResultVo<Object> insertLolBattleHeaderDocument(LolBattleHeaderVo lolBattleHeaderVo) {
        ResultVo<Object> resultVo = new ResultVo<>();
        Long gameId = lolBattleHeaderVo.getGameId();
        if (gameId == null || gameId == 0L) {
            resultVo.result("500", "对局ID不能为空！");
            return resultVo;
        }
        LolBattleHeaderPo lolBattleHeaderPo = new LolBattleHeaderPo();
        BeanUtils.copyProperties(lolBattleHeaderVo, lolBattleHeaderPo);
        lolBattleHeaderPo.setId(null);
        try {
            mongoTemplate.insert(lolBattleHeaderPo);
            resultVo.setSuccess(true);
            resultVo.result("0", "插入对局记录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "插入对局记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> updateLolBattleHeaderDocument(LolBattleHeaderVo lolBattleHeaderVo) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (StrUtil.isEmpty(lolBattleHeaderVo.getId())) {
            resultVo.result("500", "MongoDB主键不能为空！");
            return resultVo;
        }
        LolBattleHeaderPo lolBattleHeaderPo = new LolBattleHeaderPo();
        BeanUtils.copyProperties(lolBattleHeaderVo, lolBattleHeaderPo);
        try {
            mongoTemplate.save(lolBattleHeaderPo);
            resultVo.setSuccess(true);
            resultVo.result("0", "修改对局记录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "修改对局记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleHeader(Long gameId) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L) {
            resultVo.result("500", "对局ID不能为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").is(gameId));
            List<LolBattleHeaderPo> headerPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleHeaderPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局记录成功！", headerPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局记录失败！");
        }
        return resultVo;
    }
}